package cn.iocoder.yudao.module.wms.service.otherin;

import cn.iocoder.yudao.framework.common.enums.ApproveStatusEnum;
import cn.iocoder.yudao.module.base.api.ProductInfoApi;
import cn.iocoder.yudao.module.base.api.dto.ProductInfoRespDTO;
import cn.iocoder.yudao.module.wms.dal.dataobject.otherin.OtherInDDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.otherin.OtherInDO;
import cn.iocoder.yudao.module.wms.dal.mysql.otherin.OtherInMapper;
import cn.iocoder.yudao.module.wms.service.inventory.InventoryAbstract;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.OTHER_IN_NOT_APPROVE;

/**
 * @Author: Ljx
 * @createTime: 2024年05月05日 14:25:04
 * @version: v1.0.0
 * @Description: 其他入库单审核状态
 */
@Component(value = "true")
public class OtherInApproveStatusImpl extends OtherInStatusAbstract {
    @Resource
    private OtherInMapper otherInMapper;
    @Resource
    private ProductInfoApi productInfoApi;
    @Resource
    private Map<String, InventoryAbstract> inventoryMap;

    @Override
    public void approve(OtherInDO otherIn, List<OtherInDDO> otherInDs) {
        throw exception(OTHER_IN_NOT_APPROVE);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancel(OtherInDO otherIn, List<OtherInDDO> otherInDs) {
        //TODO 当前单据需要变更为未审核状态
        otherIn.setApproveStatus(ApproveStatusEnum.CANCEL.getStatus());
        otherIn.setApproveDate(null);
        otherIn.setApproveUser(null);
        otherInMapper.updateById(otherIn);
        //TODO 库存变更
        otherInDs.forEach(otherInD -> {
            ProductInfoRespDTO productInfo = productInfoApi.getProductInfo(otherInD.getProductId());
            inventoryMap.get(productInfo.getManageType().toString()).revertIncreaseInventory(otherInD.getProductId(), otherInD.getWarehouseId(),
                    otherInD.getPositionId(), otherInD.getBusinessQty(), otherInD.getBatchNo(), otherInD.getSerialNo(), otherInD.getSequence());
        });
    }
}
